package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.SellerService;
import util.MD5;

public class CreatSellerServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public CreatSellerServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		String action = request.getParameter("action");
		String result = "failed";
		try {
			// 商家注册
			if ("add".equals(action)) {
				String sellerName = request.getParameter("sellerName");
				String password = request.getParameter("password");
				String phoneNum = request.getParameter("phone");
				String seller = request.getParameter("owner");
				String sellerAddress = request.getParameter("addr");
				if (SellerService.checkSellerByName(sellerName)) {
					out.print("nameAlreadyHave");
					return;
				}
				if (SellerService.checkSellerByPhone(phoneNum)) {
					out.print("phoneAlreadyHave");
					return;
				}
				result = SellerService.creatSeller(sellerName, password, "",
						phoneNum, seller, sellerAddress, "", "", "");
				// 电话修改
			} else if ("changePhone".equals(action)) {
				String sellerId = request.getParameter("sellerId");
				String phone = request.getParameter("phone");
				if (SellerService.checkSellerByPhone(phone)) {
					result = "phoneNumIsUsed";
				} else {
					result = SellerService.changeSellerPhone(sellerId, phone);
				}
				// 密码修改
			} else if ("changePassword".equals(action)) {
				String sellerId = request.getParameter("sellerId");
				String password = request.getParameter("password");
				result = SellerService.changeSellerPassword(sellerId, password);
				if ("success".equals(result)) {
					request.getSession().setAttribute("sellerpassword",
							password);
				}
				// 验证邮箱
			} else if ("valideEmail".equals(action)) {
				String sellerId = request.getParameter("sellerId");
				String email = request.getParameter("email");
				result = SellerService.checkSellerByEmail(sellerId, email) ? "emailIsUsed"
						: "success";
				// 修改商家信息
			} else if ("changeInfo".equals(action)) {
				String sellerId = request.getParameter("sellerId");
				String deliveryTime = request.getParameter("deliveryTime");
				String addr = request.getParameter("addr");
				String email = request.getParameter("email");
				String desc =request.getParameter("desc");
				result = SellerService.changeSellerInfo(sellerId, deliveryTime, addr, email, desc);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		out.print(result);

		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
